package red.lixiang.controller;

import red.lixiang.model.BaseInfo;
import red.lixiang.model.ProductPromotion;
import red.lixiang.service.PromotionService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.logging.Logger;

@Controller
public class PromotionController {
    private final Logger logger = Logger.getLogger(PromotionController.class.getName());

    @Autowired
    private PromotionService promotionService;

    @GetMapping("/promotion/getAllPromotion")
    @ResponseBody
    public String getAllPromotion() {
        ProductPromotion promotion = new ProductPromotion();
        promotion.setPromotionType(0);
        BaseInfo baseInfo = promotionService.getAllPromotion(new ProductPromotion());
        return JSON.toJSONString(baseInfo.getObject());
    }

    @GetMapping("/promotion/setNewPromotion")
    @ResponseBody
    public String setNewPromotion(@RequestParam(required = false) String productId,
                                  @RequestParam(required = false) Integer type) {

        ProductPromotion promotion = new ProductPromotion();
        promotion.setProductId(productId);
        promotion.setPromotionType(type);

        promotionService.setNewPromotion(promotion);

        return "OK";
    }
}
